package com.incar.chuxing.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.incar.chuxing.R;
import com.incar.chuxing.activity.DeliverCarActivity;
import com.incar.chuxing.bean.AbatementMoneyBean;
import com.incar.chuxing.utils.Logger;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by 17310 on 2018/10/15.
 */

public class DeliverRvAdapter extends BaseQuickAdapter<AbatementMoneyBean.AttachmentBean.GradeListBean, BaseViewHolder> {
    private List<AbatementMoneyBean.AttachmentBean.GradeListBean> listdata;
    private Context mContext;
    private DeliverCarActivity deliverCarActivity;

    public DeliverRvAdapter(List<AbatementMoneyBean.AttachmentBean.GradeListBean> data, Context context) {
        super(R.layout.item_deliver_rv, data);
        listdata = data;
        mContext = context;
        deliverCarActivity = (DeliverCarActivity) context;
    }

    @Override
    protected void convert(BaseViewHolder helper, AbatementMoneyBean.AttachmentBean.GradeListBean item) {
        helper.setText(R.id.item_deliver_rv_tv, item.getGradeName());
        helper.setOnCheckedChangeListener(R.id.checkbox, new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Logger.i("deliver", "点击了checkbox" + isChecked);
                item.setChecked(isChecked);
                //更新不计免赔价格
                List<Double> list = new ArrayList<>();
                for (int i = 0; i < listdata.size(); i++) {
                    if (listdata.get(i).isChecked()) {
                        list.add(Double.parseDouble(listdata.get(i).getAbatementMoney()));
                    }
                }
                if (list.size() > 0) {
                    double macAbatementMoney = Collections.max(list);
                    if (deliverCarActivity.rentType == 2) {
                        deliverCarActivity.carDetailInsureTv.setText(new DecimalFormat("不计免赔(#.##元/天)").format(macAbatementMoney));
                    } else {
                        deliverCarActivity.carDetailInsureTv.setText(new DecimalFormat("不计免赔(#.##元/小时)").format(macAbatementMoney));
                    }
                } else {
                    if (deliverCarActivity.rentType == 2) {
                        deliverCarActivity.carDetailInsureTv.setText("不计免赔(-元/天)");
                    } else {
                        deliverCarActivity.carDetailInsureTv.setText("不计免赔(-元/小时)");
                    }
                }
            }
        });
    }
}
